package com.cskaoyan.market.util;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.db.mapper.MarketLogMapper;

/**
 * @author 小焦
 * @since 2023/03/20 15:49
 */
public class RequestUtil {

	public static BasePageInfo getBasePageInfo(HttpServletRequest req){
		Integer page = Integer.valueOf(req.getParameter("page"));
		Integer limit = Integer.valueOf(req.getParameter("limit"));
		String sort = req.getParameter("sort");
		String order = req.getParameter("order");
		return new BasePageInfo(limit,page,order,sort);
	}

	public static void insertMarketLogByAdmin(HttpServletRequest req, String action, String result) {
		HttpSession session = req.getSession();
		MarketAdmin adminInfo = (MarketAdmin) session.getAttribute("info");
		String username = adminInfo.getUsername();
		String lastLoginIp = adminInfo.getLastLoginIp();
		SqlSession se = MyBatisUtil.getSession();
		MarketLogMapper logMapper = se.getMapper(MarketLogMapper.class);
		MarketLog marketLog = new MarketLog();
		LocalDateTime now = LocalDateTime.now();
		marketLog.setAdmin(username);
		marketLog.setAddTime(now);
		marketLog.setDeleted(false);
		marketLog.setIp(lastLoginIp);
		marketLog.setAction(action);
		marketLog.setType(1);
		marketLog.setStatus(true);
		marketLog.setResult(result);
		logMapper.insertSelective(marketLog);
		se.commit();
		se.close();
	}

	public static void insertMarketLogByAuth(HttpServletRequest req, String action, String result, String username) {
		HttpSession session = req.getSession();
		MarketAdmin adminInfo = (MarketAdmin) session.getAttribute("info");
		String lastLoginIp = adminInfo.getLastLoginIp();
		SqlSession se = MyBatisUtil.getSession();
		MarketLogMapper logMapper = se.getMapper(MarketLogMapper.class);
		MarketLog marketLog = new MarketLog();
		LocalDateTime now = LocalDateTime.now();
		marketLog.setAdmin(username);
		marketLog.setAddTime(now);
		marketLog.setDeleted(false);
		marketLog.setIp(lastLoginIp);
		marketLog.setAction(action);
		marketLog.setType(1);
		marketLog.setStatus(true);
		marketLog.setResult(result);
		logMapper.insertSelective(marketLog);
		se.commit();
		se.close();
	}
}
